﻿using Castle.DynamicProxy;
using System;
using System.IO;
using System.Linq;

namespace MYData.WebApi.AOP
{
        //<summary>
        //拦截器BlogLogAOP继承IInterceptor接口
        //</summary>
        public class ProjectLogAOP : IInterceptor
        {
            /// <summary>
            /// 实例化IInterceptor唯一方法
            /// </summary>
            /// <param name="invocation">包含拦截方法的信息</param>
            public void Intercept(IInvocation invocation)
            {
                // 记录被拦截方法的日志信息
                var dataInterceptor = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}" +
                    $"当前执行方法:{invocation.Method.Name} " +
                    $"参数是: {string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())} \r\n";

                // 在被拦截的方法执行完毕后 继续执行当前方法
                invocation.Proceed();

                dataInterceptor += ($"方法执行完毕,返回结果:{invocation.ReturnValue}");

                #region 输出当前项目日志
                var path = Directory.GetCurrentDirectory() + @"\Log";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string fileName = path + $@"\InterceptLog-{DateTime.Now.ToString("yyyyMMdd")}.log";
                StreamWriter sw = File.AppendText(fileName);
                sw.WriteLine(dataInterceptor);
                sw.Close();
                #endregion
            }
        }
    
}
